﻿Public Class TheTinDungForm
    Dim dt As DataTable
    Dim dt_nganhang As DataTable
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Me.Dispose()
    End Sub

    Private Sub NganHangForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dt_nganhang = NganHangDAO.GetAll()
        cbMaNganHang.DataSource = dt_nganhang
        cbMaNganHang.DisplayMember = "Ten"
        cbMaNganHang.ValueMember = "MaNganHang"

        dt = TheTinDungDAO.GetAll(MainForm.current_user.MaUser)
        dg.DataSource = dt
        dg.Columns("MaTheTinDung").HeaderText = "Mã Thẻ Tín Dụng"
        dg.Columns("HanMuc").HeaderText = "Hạn Mức"
        dg.Columns("LaiSuat").HeaderText = "Lãi Suất"
        dg.Columns("Ten").HeaderText = "Tên Ngân Hàng"
        dg.Columns("Ten").DisplayIndex = 2
        dg.Columns("ID").Visible = False
        dg.Columns("MaUser").Visible = False
        dg.Columns("MaNganHang").Visible = False
        dg.Columns("MaNganHang1").Visible = False

        Dim btn As New DataGridViewButtonColumn()        
        btn.HeaderText = ""
        btn.Text = "Chi Tiết"
        btn.Name = "btn"
        btn.UseColumnTextForButtonValue = True
        btn.DisplayIndex = 1        
        dg.Columns.Add(btn)
    End Sub

    Private Sub dgNganHang_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg.CellClick
        If dg.Rows.Count > 0 Then
            If dg.CurrentCell.Value.ToString() = "Chi Tiết" Then
                Me.Hide()
                Dim mf As New DSChiTietSuDungTheForm(Me, dg.CurrentRow.Cells("MaTheTinDung").Value)
                mf.MdiParent = MainForm
                mf.Show()
            Else
                txtMaTheTinDung.Text = dt.Rows(dg.CurrentRow.Index).Item("MaTheTinDung").ToString()
                txtLaiSuat.Text = dt.Rows(dg.CurrentRow.Index).Item("LaiSuat").ToString()
                txtHanMuc.Text = dt.Rows(dg.CurrentRow.Index).Item("HanMuc").ToString()
                cbMaNganHang.SelectedValue = dt.Rows(dg.CurrentRow.Index).Item("MaNganHang").ToString()
            End If
        End If        
    End Sub

    Private Sub btThem_Click(sender As System.Object, e As System.EventArgs) Handles btThem.Click
        If txtMaTheTinDung.Text.Trim().Length = 0 Then
            MessageBox.Show("Xin vui lòng nhập vào mã thẻ tín dụng")
            Return
        End If
        If txtHanMuc.Text.Trim().Length = 0 Or Not IsNumeric(txtHanMuc.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào hạn mức")
            Return
        End If
        If txtLaiSuat.Text.Trim().Length = 0 Or Not IsNumeric(txtLaiSuat.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào lãi suất")
            Return
        End If
        If cbMaNganHang.SelectedIndex < 0 Then
            MessageBox.Show("Xin vui lòng chọn ngân hàng")
            Return
        End If
        Dim dr As DataRow = dt.NewRow()
        dr("MaUser") = MainForm.current_user.MaUser
        dr("MaNganHang") = cbMaNganHang.SelectedValue
        dr("MaTheTinDung") = txtMaTheTinDung.Text
        dr("HanMuc") = Double.Parse(txtHanMuc.Text)
        dr("LaiSuat") = Double.Parse(txtLaiSuat.Text)
        dr("Ten") = dt_nganhang.Rows(cbMaNganHang.SelectedIndex)("Ten").ToString()
        dt.Rows.Add(dr)
        DB_Provider.Write(dt, "TheTinDung")
    End Sub

    Private Sub btXoa_Click(sender As System.Object, e As System.EventArgs) Handles btXoa.Click
        If dt.Rows.Count > 0 Then
            dt.Rows(dg.CurrentRow.Index).Delete()
            DB_Provider.Write(dt, "TheTinDung")
        End If
    End Sub

    Private Sub btSua_Click(sender As System.Object, e As System.EventArgs) Handles btSua.Click
        If txtMaTheTinDung.Text.Trim().Length = 0 Or txtMaTheTinDung.Text.Trim().Length > 20 Then
            MessageBox.Show("Xin vui lòng nhập vào mã thẻ tín dụng tối đa 20 ký tự")
            Return
        End If
        If txtHanMuc.Text.Trim().Length = 0 Or Not IsNumeric(txtHanMuc.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào hạn mức")
            Return
        End If
        If txtLaiSuat.Text.Trim().Length = 0 Or Not IsNumeric(txtLaiSuat.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào lãi suất")
            Return
        End If
        If cbMaNganHang.SelectedIndex < 0 Then
            MessageBox.Show("Xin vui lòng chọn ngân hàng")
            Return
        End If
        Dim index As Integer = dg.CurrentRow.Index
        If (index > -1) Then
            dt.Rows(index).Item("HanMuc") = txtHanMuc.Text
            dt.Rows(index).Item("MaNganHang") = cbMaNganHang.SelectedValue
            dt.Rows(index).Item("Ten") = dt_nganhang.Rows(cbMaNganHang.SelectedIndex)("Ten").ToString()
            dt.Rows(index).Item("LaiSuat") = txtLaiSuat.Text
            DB_Provider.Write(dt, "TheTinDung")
        End If
    End Sub

    Private Sub dg_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg.CellDoubleClick
        If dg.Rows.Count > 0 Then
            Me.Hide()
            Dim mf As New DSChiTietSuDungTheForm(Me, dg.CurrentRow.Cells("MaTheTinDung").Value)
            mf.MdiParent = MainForm
            mf.Show()
        End If        
    End Sub

    Private Sub btThemNganHang_Click(sender As System.Object, e As System.EventArgs) Handles btThemNganHang.Click
        Dim frm As New NganHangForm()
        If frm.ShowDialog() = DialogResult.OK Then
            dt_nganhang = NganHangDAO.GetAll()
            cbMaNganHang.DataSource = dt_nganhang
        End If
    End Sub
End Class